import Util.HttpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static Util.Indicator.setIndicatorObject;
import static Util.Rules.disable_Indicator;
import static Util.Rules.specialCleaning;
import static Util.SelectIndicator.selectIndicatorObject;
import static Util.UrlCollect.*;

public class AddIndicatorObject {

    public static void main(String[] args) throws IOException {
        //正式环境


        HttpUtil http = new HttpUtil();
        Scanner scanner = new Scanner(System.in);

        Map<String, String> paramsMap = new HashMap<>();
        String params1 = "{\"pageNum\": 1, \"pageCount\": 2000}";

        //获取所有模板
        JSONObject jsonObject1 = http.doBodyPost(GET_ALL_TEMPLATES_URL, params1);
        JSONArray array = jsonObject1.getJSONObject("data").getJSONArray("data");
        for (Object o1 : array) {
            JSONObject json4 = (JSONObject) o1;
            String m_name = json4.getString("name");
            if (json4.getIntValue("id") >= 88) {
                continue;
            }
            System.out.println("=======================" + m_name + "=======================");
            //模板id
            paramsMap.put("id", json4.get("id").toString());
//            paramsMap.put("id", "69");

            //判断模板类型
            String M_TYPE = "";
            if (m_name.contains("分院模板") && m_name.contains("一年版")) {
                M_TYPE = "一年版分院报告";
            } else if (m_name.contains("分院模板") || m_name.contains("一年版")) {
                M_TYPE = m_name.contains("分院模板") ? "分院报告" : "一年版";
            }
            if (m_name.contains("专科")) {
                M_TYPE += "专科";
            } else {
                M_TYPE += "本科";
            }

            //根据模板ID 获取所有大纲id
            String res = http.doParamsPost(OBTAIN_OUTLINE_URL, paramsMap);
            JSONObject object = JSONObject.parseObject(res);
            for (Object json : object.getJSONArray("data")) {
                //单个大纲 object1
                JSONObject object1 = (JSONObject) json;
                int key = object1.getIntValue("key");
                String pa = "{\"id\":" + key + "}";
                JSONObject s = null;
                try {
                    s = http.doBodyPost(GET_DETAILS_URL, pa);
                    // 单个大纲配置的总体正文及指标对象  s1
                    JSONArray s1 = s.getJSONArray("data");
                    for (Object json1 : s1) {
                        //单个标题坑位 json2
                        JSONObject json2 = (JSONObject) json1;
                        JSONArray data = json2.getJSONArray("data");
                        if (data != null) {
                            for (Object s2 : data) {
                                //单个正文或指标对象 s21
                                JSONObject s21 = (JSONObject) s2;
                                if (s21.getString("type").equals("UNKNOWN_INDICATOR_OBJECT")) {
                                    //指标对象名称 titleFootnote
                                    String titleFootnote = removeParentheses(json2.getString("titleFootnote"));
                                    // 跳过禁用置空的指标对象
                                    if (disable_Indicator(titleFootnote)) continue;

                                    // 特殊名称清理
                                    if (titleFootnote.equals("部分缺失专业毕业生的能力达成及样本") || titleFootnote.equals("部分缺失专业毕业生的就业质量主要指标及样本")) {
                                        if (m_name.contains("研究型")) {
                                            titleFootnote += "（研究型）";
                                        } else {
                                            titleFootnote += "（非研究型）";
                                        }
                                    }
                                    if (specialCleaning.containsKey(titleFootnote)) {
                                        if (m_name.contains("专科") || ((!titleFootnote.contains("类")) | titleFootnote.contains("“"))) {
                                            titleFootnote = specialCleaning.get(titleFootnote);
                                        }
                                    }
                                    if (titleFootnote.equals("各学院临床药学类能力达成度")) titleFootnote = "各学院临床药学能力达成度";

                                    //跳过
                                    if (titleFootnote.contains("xxx")) continue;
                                    if (titleFootnote.contains("本地就业的主要行业类")) continue;
                                    if (titleFootnote.contains("本地就业的主要职业类")) continue;

                                    boolean flag =true;
                                    while (flag){
                                    //指标对象搜索结果  data0  自动清理指标对象名称
                                    JSONArray data0 = selectIndicatorObject(titleFootnote, M_TYPE);
                                    //原始指标名称
                                    String titleFootnote1 = removeParentheses(json2.getString("titleFootnote"));

                                    switch (data0.size()) {
                                        case 1: //精确匹配
                                            JSONObject r = (JSONObject) data0.get(0);
                                            System.out.println("指标名称: "+titleFootnote1);
                                            System.out.println("匹配名称: "+r.getString("name"));
                                            //插入请求
                                            System.out.println("精确匹配: "+setIndicatorObject(r.getString("id"), json2.getString("outlineId")));
                                            flag=false;
                                            break;
                                        case 0: //没有匹配成功
                                            System.out.println("!!!!!!!!!!!!!!!!!!!");
                                            System.out.println("指标名称：" + titleFootnote);
                                            System.out.println("!!!!!!!!!!!!!!!!!!!");
                                            scanner.next();
                                            break;
                                        default://多项匹配
                                            int i=1;
                                            System.out.println("================================");
                                            System.out.println(titleFootnote1);
                                            for (Object o : data0) {
                                                JSONObject jsonObject = (JSONObject) o;
                                                System.out.println(i+++": "+jsonObject.getString("name"));
                                            }
                                            System.out.println("请输入选择的指标对象:");
                                            int choose = scanner.nextInt();

                                            switch (choose){
                                                case 0:  //输入 0 跳过选择
                                                    flag=false;
                                                    break;
                                                case -1: //输入 -1 重新选择
                                                    System.out.println("请输入新搜索的指标名称:");
                                                    titleFootnote= scanner.next();
                                                    break;
                                                default://输入 序号 确定选择
                                                    JSONObject o = (JSONObject) data0.get(choose - 1);
                                                    System.out.println(o.getString("name"));
                                                    //插入请求
                                                    setIndicatorObject(o.getString("id"),json2.getString("outlineId"));
                                                    flag=false;
                                                    break;
                                            }
                                            break;
                                    }
                                }
                                }
                            }
                        }
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String removeParentheses(String input) {
        Pattern pattern = Pattern.compile("\\{([^{}])*\\}");
        Matcher matcher = pattern.matcher(input);
        return matcher.replaceAll("").trim();
    }
}